package org.dromara.Nyt7InsuranceProduct.controller;

import java.util.Collections;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.Nyt7InsuranceProduct.domain.Nyt7InsuranceProduct;
import org.dromara.Nyt7InsuranceProduct.domain.Nyt7InsuranceProduct1;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.Nyt7InsuranceProduct.domain.vo.Nyt7InsuranceProductVo;
import org.dromara.Nyt7InsuranceProduct.domain.bo.Nyt7InsuranceProductBo;
import org.dromara.Nyt7InsuranceProduct.service.INyt7InsuranceProductService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 保险产品
 *
 * @author yt
 * @date 2025-01-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/Nyt7InsuranceProduct/Nyt7InsuranceProduct")
public class Nyt7InsuranceProductController extends BaseController {

    private final INyt7InsuranceProductService nyt7InsuranceProductService;

    /**
     * 查询保险产品列表
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:list")
    @GetMapping("/list")
    public TableDataInfo<Nyt7InsuranceProductVo> list(Nyt7InsuranceProductBo bo, PageQuery pageQuery) {
        return nyt7InsuranceProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出保险产品列表
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:export")
    @Log(title = "保险产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Nyt7InsuranceProductBo bo, HttpServletResponse response) {
        List<Nyt7InsuranceProductVo> list = nyt7InsuranceProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "保险产品", Nyt7InsuranceProductVo.class, response);
    }

    /**
     * 获取保险产品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:query")
    @GetMapping("/{id}")
    public R<Nyt7InsuranceProduct> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(nyt7InsuranceProductService.queryById(id));
    }

    /**
     * 新增保险产品
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:add")
    @Log(title = "保险产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Nyt7InsuranceProductBo bo) {
        return toAjax(nyt7InsuranceProductService.insertByBo(bo));
    }

    /**
     * 修改保险产品
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:edit")
    @Log(title = "保险产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Nyt7InsuranceProductBo bo) {
        return toAjax(nyt7InsuranceProductService.updateByBo(bo));
    }

    /**
     * 删除保险产品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:remove")
    @Log(title = "保险产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(nyt7InsuranceProductService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取保险产品总数
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:list")
    @GetMapping("/count")
    public R<Long> getCount() {
        return R.ok(nyt7InsuranceProductService.getCount());
    }

    @Log(title = "保险公司", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Boolean> excelImport(MultipartFile file) throws Exception {
        // 使用静态方法导入Excel文件
        List<Nyt7InsuranceProduct> list = ExcelUtil.importExcel(file.getInputStream(), Nyt7InsuranceProduct.class);

        // 处理导入的数据（保存到数据库）

        return R.ok( nyt7InsuranceProductService.saveBatch(list));
    }



    /**
     * 导出选中的保险产品列表
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:export")
    @Log(title = "保险产品", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelected")
    public void exportSelected(@RequestBody List<Long> ids, HttpServletResponse response) {
        List<Nyt7InsuranceProductVo> list = nyt7InsuranceProductService.queryListByIds(ids);
        ExcelUtil.exportExcel(list, "保险产品", Nyt7InsuranceProductVo.class, response);
    }

    /**
     * 导入模版下载
     */
    @SaCheckPermission("Nyt7InsuranceProduct:Nyt7InsuranceProduct:export")
    @Log(title = "保险产品", businessType = BusinessType.EXPORT)
    @PostMapping("/template")
    public void template(HttpServletResponse httpServletResponse){
        List<Nyt7InsuranceProduct> emptyList = Collections.emptyList();
        ExcelUtil.exportExcel(emptyList,"保险产品-导入模版", Nyt7InsuranceProduct.class,httpServletResponse);
    }

}
